package com.jiyinit.web.core.business.dao;

import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.dao.mybatis.base.MybatisDaoContext;
import com.jiyinit.core.framework.dao.mybatis.paginator.domain.PageBounds;
import com.jiyinit.web.core.business.dto.MessagePageRequest;
import com.jiyinit.web.core.business.dto.MessageRequest;
import com.jiyinit.web.core.business.dto.ReadMessageRequest;
import com.jiyinit.web.core.business.entity.TabCoreFunEntity;
import com.jiyinit.web.core.business.entity.TabCoreTask;
import com.jiyinit.web.core.business.entity.TabCoreTaskMessage;
import com.jiyinit.web.core.business.entity.TabSendMessageEntity;
import com.jiyinit.web.core.business.vo.SendMessageVo;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

/**
 * <p></p>
 * <p/>
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author zhaolaiwei
 * @version 1.0
 * @date 2017/4/11 17:51
 * @since 1.0
 */
@Repository("send.tabSendMessageDao")
public class TabSendMessageDao {

    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(TabSendMessageDao.class);

    private String SQLID = "send.messageDao.";

    @Autowired
    @Qualifier("core.MybatisDaoContext")
    private MybatisDaoContext mybatisDaoContext;

    public List<TabSendMessageEntity> findNoReadMessage(Integer userId){
        return mybatisDaoContext.findAll(SQLID+"findNoReadMessage",TabSendMessageEntity.class,userId);
    }

    public int batchInsert(List<TabSendMessageEntity> list){
        SqlSessionTemplate sqlSessionTemplate = mybatisDaoContext.getWriteSqlSessionTemplate();
        return sqlSessionTemplate.insert(SQLID + "batchSave", list);
    }

    public int updateReadStatus(ReadMessageRequest readMessageRequest){
        HashMap<String,Object> params = new HashMap<>();
        params.put("taskId",readMessageRequest.getTaskId());
        params.put("userId",readMessageRequest.getUserId());
        return mybatisDaoContext.update(SQLID+ "updateReadStatus",params);
    }

    public PagingResult<SendMessageVo> readByPage(MessagePageRequest request){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(request.getLimit());
        pageBounds.setPage(request.getPage());

        HashMap<String,Object> params = new HashMap<>();
        params.put("funId",request.getFunId());
        params.put("id",request.getId());

        return mybatisDaoContext.findForPage(SQLID+"readByPage",SendMessageVo.class,params,pageBounds);
    }
}
